//
//  YSGHelpCenterViewController.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/11/18.
//  Copyright © 2020 QDSG. All rights reserved.
//

import UIKit
/// 帮助中心
class YSGHelpCenterViewController: BaseViewController {
    
    private var viewModel: YSGHelpCenterCategoryViewModel? {
        didSet {
            tableView.isHidden = false
            tableView.reloadData()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        loadData()
    }
    
    private var isPush = false
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        isPush = false
        navigationController?.setNavigationBarHidden(true, animated: true)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        setNavClear()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        if isPush {
            setNavWhite()
            navigationController?.setNavigationBarHidden(false, animated: true)
        }
    }
    
    override func makeUI() {
        super.makeUI()
        view.addSubview(topHeaderView)
        topHeaderView.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(180)
        }
        topHeaderView.handleClosure = { [weak self] isBack in
            if !isBack {
                self?.pushSearchQuestionVc()
            } else {
                self?.navigationController?.popToRootViewController(animated: true)
            }
        }
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.top.equalTo(topHeaderView.snp.bottom).offset(-32)
            make.left.right.bottom.equalToSuperview()
        }
    }

    private lazy var tableView: UITableView = {
        let view = UITableView(frame: .zero, style: .grouped)
        view.delegate = self
        view.dataSource = self
        view.isHidden = true
        view.separatorStyle = .none
        view.tableFooterView = UIView()
        view.backgroundColor = .clear
        view.register(YSGHelpCenterCategoryCell.nibForCell(),
                      forCellReuseIdentifier: YSGHelpCenterCategoryCell.reuseIdentifier())
        view.register(YSGHelpCenterFeedbackCell.nibForCell(),
                      forCellReuseIdentifier: YSGHelpCenterFeedbackCell.reuseIdentifier())
        return view
    }()
    
    private lazy var topHeaderView = UINib.loadViewFrom(YSGHelpCenterNavBarView.self)

    private func loadData() {
        HUDTool.showActivity()
        YSGHelpCenterCategoryViewModel.getHelpCenterCategory { (models, message, code) in
            HUDTool.hideActivity()
            switch code {
            case .failure:
                HUDTool.showError(message: message)
            case .success:
                self.viewModel = YSGHelpCenterCategoryViewModel(models: models)
            }
        }
    }
}

private extension YSGHelpCenterViewController {
    
    func pushSearchQuestionVc() {
        let searchQuestion = YSGHelpCenterQuestionListController(isSearch: true)
        navigationController?.pushViewController(searchQuestion, animated: true)
    }
}

extension YSGHelpCenterViewController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch indexPath.section {
        case 0:
            let cell = tableView.dequeueReusableCell(withIdentifier: YSGHelpCenterCategoryCell.reuseIdentifier(), for: indexPath) as! YSGHelpCenterCategoryCell
            cell.delegate = self
            if let vm = viewModel {
                cell.configure(with: vm)
            }
            return cell
        case 1:
            let cell = tableView.dequeueReusableCell(withIdentifier: YSGHelpCenterFeedbackCell.reuseIdentifier(), for: indexPath) as! YSGHelpCenterFeedbackCell
            return cell
        default:
            return UITableViewCell()
        }
    }
}

extension YSGHelpCenterViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        switch indexPath.section {
        case 0: return viewModel?.cellHeight() ?? 0
        default: return 50
        }
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.0001
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 16.0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        switch indexPath.section {
        case 1:
            isPush = true
            pushFeedbackVc()
        default:
            break
        }
    }
}

extension YSGHelpCenterViewController: YSGHelpCenterCategoryCellDelegate {
    func categoryCell(didSelectedWith vm: YSGHelpCenterCategoryItemViewModel) {
        isPush = true
        pushQuestionList(with: vm)
    }
    
    private func pushQuestionList(with vm: YSGHelpCenterCategoryItemViewModel) {
        let questionList = YSGHelpCenterQuestionListController(code: vm.model.code ?? "", name: vm.model.name ?? "", isSearch: false)
        navigationController?.pushViewController(questionList, animated: true)
    }
    
    private func pushFeedbackVc() {
        let feedbackVc = YSGHelpCenterFeedbackController()
        navigationController?.pushViewController(feedbackVc, animated: true)
    }
}
